
/* Includes ------------------------------------------------------------------*/
#include "adc.h"

/* USER CODE BEGIN 0 */
#include <stdio.h>

uint16_t calculate_aver(uint16_t usIn[], uint16_t usCnt);

static uint16_t   aADCxConvertedData[ADC_CONVERTED_DATA_BUFFER_SIZE];
/* USER CODE END 0 */

ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc1;

/* ADC1 init function */
void MX_ADC1_Init(void)
{
  ADC_ChannelConfTypeDef sConfig = {0};

  /** Common config 
  */
  hadc1.Instance = ADC1;
  hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
  hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  hadc1.Init.LowPowerAutoWait = DISABLE;
  hadc1.Init.ContinuousConvMode = ENABLE;
  hadc1.Init.NbrOfConversion = 3;
  hadc1.Init.DiscontinuousConvMode = DISABLE;
  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc1.Init.DMAContinuousRequests = ENABLE;
  hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  hadc1.Init.OversamplingMode = DISABLE;
  if (HAL_ADC_Init(&hadc1) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure Regular Channel 
  */
  sConfig.Channel = ADC_CHANNEL_5;
  sConfig.Rank = ADC_REGULAR_RANK_1;
  sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
  sConfig.SingleDiff = ADC_SINGLE_ENDED;
  sConfig.OffsetNumber = ADC_OFFSET_NONE;
  sConfig.Offset = 0;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure Regular Channel 
  */
  sConfig.Channel = ADC_CHANNEL_9;
  sConfig.Rank = ADC_REGULAR_RANK_2;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure Regular Channel 
  */
  sConfig.Channel = ADC_CHANNEL_11;
  sConfig.Rank = ADC_REGULAR_RANK_3;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }

}

void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(adcHandle->Instance==ADC1)
  {
  /* USER CODE BEGIN ADC1_MspInit 0 */

  /* USER CODE END ADC1_MspInit 0 */
    /* ADC1 clock enable */
    __HAL_RCC_ADC_CLK_ENABLE();
  
    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**ADC1 GPIO Configuration    
    PA0     ------> ADC1_IN5
    PA4     ------> ADC1_IN9
    PA6     ------> ADC1_IN11 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_4|GPIO_PIN_6;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG_ADC_CONTROL;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* ADC1 DMA Init */
    /* ADC1 Init */
    hdma_adc1.Instance = DMA1_Channel1;
    hdma_adc1.Init.Request = DMA_REQUEST_0;
    hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
    hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
    hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
    hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
    hdma_adc1.Init.Mode = DMA_CIRCULAR;
    hdma_adc1.Init.Priority = DMA_PRIORITY_LOW;
    if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)
    {
      Error_Handler();
    }

    __HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc1);

  /* USER CODE BEGIN ADC1_MspInit 1 */

  /* USER CODE END ADC1_MspInit 1 */
  }
}

void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
{

  if(adcHandle->Instance==ADC1)
  {
  /* USER CODE BEGIN ADC1_MspDeInit 0 */

  /* USER CODE END ADC1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_ADC_CLK_DISABLE();
  
    /**ADC1 GPIO Configuration    
    PA0     ------> ADC1_IN5
    PA4     ------> ADC1_IN9
    PA6     ------> ADC1_IN11 
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0|GPIO_PIN_4|GPIO_PIN_6);

    /* ADC1 DMA DeInit */
    HAL_DMA_DeInit(adcHandle->DMA_Handle);
  /* USER CODE BEGIN ADC1_MspDeInit 1 */

  /* USER CODE END ADC1_MspDeInit 1 */
  }
} 

/* USER CODE BEGIN 1 */
void ADC_ReInit(void)
{
	HAL_ADC_MspInit(&hadc1);
}

void ADC_Start(void)
{
	#if 1
	if (HAL_ADC_Start_DMA(&hadc1,
											(uint32_t *)aADCxConvertedData,
											ADC_CONVERTED_DATA_BUFFER_SIZE
										 ) != HAL_OK)
	{
		Error_Handler();
	}
	#else
	if (HAL_ADC_Start(&hadc1) != HAL_OK)
  {
    /* Start Conversation Error */
    Error_Handler();
  }
	#endif
}

void ADC_Stop(void)
{
	#if 1
	HAL_ADC_Stop_DMA(&hadc1);
	#else
	HAL_ADC_Stop(&hadc1);
	#endif
}

int ADC_Battery_Voltage(void)
{
	uint32_t i = 0, val = 0;
	
	#if 1
	uint16_t ausVal[ADC_VAL_BUFFER_SIZE] = {0};
	/*
	uint32_t pos = 0, len = 0;
	char buf[512] = {0};
	for(i = 0; i < ADC_CONVERTED_DATA_BUFFER_SIZE; i++){
		val = aADCxConvertedData[i];
		len = sprintf(buf+pos, "%d,", val); pos += len;
		
	}
	printf("ADC:%s\n",buf);
	*/
	for(i = 0; i < ADC_CONVERTED_DATA_BUFFER_SIZE; i+=3){
		ausVal[val++]= aADCxConvertedData[i];
		if(ADC_VAL_BUFFER_SIZE == val) break;
	}
	val = calculate_aver(ausVal, ADC_VAL_BUFFER_SIZE);
	#else
	val = aADCxConvertedData[i];
	#endif
	val = (float)((val * 825)>>10)*3;//(val * 3300)/4096;//(val*2.17f);//(((3.3f*val)/4096.0f)*2.5f)*1000;
	return val;
}

int ADC_Light_Val(void)
{
	uint32_t i = 1, val = 0;
	//uint32_t iLux = 0;
	#if 1
	uint16_t ausVal[ADC_VAL_BUFFER_SIZE] = {0};
	for(i = 1; i < ADC_CONVERTED_DATA_BUFFER_SIZE; i+=3){
		ausVal[val++]= aADCxConvertedData[i];
		if(ADC_VAL_BUFFER_SIZE == val) break;
	}
	val = calculate_aver(ausVal, ADC_VAL_BUFFER_SIZE);
	#else
	val = aADCxConvertedData[i];
	#endif
	//val = (((3.3f*val)/4096.0f)*2*1.5f)*1000;
	//iLux =  (val/ 10000.0f)* 1000000*2.0f;
	return val;
}

int ADC_Sound_Val(void)
{
	uint32_t i = 2, val = 0;
	#if 1
	uint16_t ausVal[ADC_VAL_BUFFER_SIZE] = {0};
	for(i = 2; i < ADC_CONVERTED_DATA_BUFFER_SIZE; i+=3){
		ausVal[val++]= aADCxConvertedData[i];
		if(ADC_VAL_BUFFER_SIZE == val) break;
	}
	val = calculate_aver(ausVal, ADC_VAL_BUFFER_SIZE);
	#else
	val = aADCxConvertedData[i];
	#endif
	val = (((3.3f*val)/4096.0f)*2*1.5f)*1000;
	return val;
}

uint16_t calculate_aver(uint16_t usIn[], uint16_t usCnt)
{
	uint16_t i = 0, val = 0;
	uint16_t max = 0, min = 0, sum = 0, aver = 0;
	if(3 > usCnt) return 0;
	max=usIn[0];
	min=usIn[0];
	
	for(i=0;i<10;i++){
		val = usIn[i];
		if(max < val)
			max = val;
		
		if(min > val)
			min = val;
		sum = sum+val;
	}
	aver=(sum-max-min)/(usCnt-2);
	return aver;
}
/* USER CODE END 1 */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
